<template>
  <div class="approval">
    <el-card style="margin-top: 10px;">
      <div class="title">{{$t('achievements.title1')}}</div>
      <div class="menu-search">
        <div class="menu-title">
          <!-- 日期 -->
          <div class="searchItem searchItem-date">
            <span>{{$t('approvalDetail.riskTime')}}</span>
            <el-date-picker
              size="small"
              v-model="listQuery.dateStr"
              type="month"
              value-format="yyyy-MM"
              :placeholder="$t('rule.beginDate')"
            ></el-date-picker>
          </div>
          <!-- 电销组 -->
          <div class="searchItem">
            <span>{{$t('elecAchievement.ElectricGroup')}}</span>
            <el-select
              v-model="listQuery.groupId"
              size="small"
              :placeholder="$t('elecAchievement.ChooseElecGroup')"
               @change="handleElecGroupChange"
            >
              <el-option
                v-for="item in salesGroupIds"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </div>
          <!-- 电销员 -->
          <div class="searchItem">
            <span>{{$t('electricRecord.ElecSalesman')}}</span>
            <el-select
              v-model="listQuery.checkUserId"
              size="small"
              :placeholder="$t('elecAchievement.ChooseElecSalesman')"
            >
              <el-option
                v-for="item in salesGroupMemberIds"
                :key="item.userId"
                :label="item.username"
                :value="item.userId"
              ></el-option>
            </el-select>
          </div>
          <div class="menu-title-btn">
            <button type="button" @click="search">{{$t('btn.query')}}</button>
            <button type="button" @click="reset">{{$t('btn.reset')}}</button>
          </div>
        </div>
      </div>
    </el-card>
    <el-card style="margin-top: 10px;margin-bottom: 20px;">
      <div class="title">{{$t('approval.title2')}}</div>
      <el-table
        v-loading="listLoading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
        size="mini"
      >
        <!-- 日期 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="reportDate"
          :label="$t('approvalDetail.riskTime')"
        ></el-table-column>
        <!-- 组名称 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="groupName"
          :label="$t('ApprReport.firstGroup')"
        ></el-table-column>
        <!-- 电销员 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="checkUserName"
          :label="$t('electricRecord.ElecSalesman')"
        ></el-table-column>
        <!-- 分配量 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="distributionNum"
          :label="$t('elecAchievement.AllocationQuan')"
        ></el-table-column>
        <!-- 接通量 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="connectedNum"
          :label="$t('elecAchievement.ConnectVolume')"
        ></el-table-column>
        <!-- 完成量 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="finishedNum"
          :label="$t('elecAchievement.CompleteQuan')"
        ></el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getGroupListByType, getUsersByGroupId, report } from "api/elecAchievement/index";
export default {
  data() {
    return {
      listLoading: false,
      total: null,
      listQuery: {
        limit: 5,
        page: 1,
        dateStr: "",
        // 电销组ID
        groupId: "",
        // 电销员ID
        checkUserId: "",
        type: 2
      },
      tableData: [],
      // 电销组ID
      salesGroupIds: [],
      // 电销员ID
      salesGroupMemberIds: []
    };
  },
  created() {
    if (this.ElecSalesmanPage.has(this.$route.path)) {
      this.listQuery = this.ElecSalesmanPage.get(this.$route.path);
    }
    this.getList();
    // 电销组
    let obj = {};
    obj.code = 'telemarketing';
    getGroupListByType(obj).then(res => {
      if (res.status === 200) {
        this.salesGroupIds = res.data;
      }
    });
  },
  computed: {
    ...mapGetters(["elements", "ElecSalesmanPage"])
  },
  methods: {
    // 电销组联动
    handleElecGroupChange(val) {
      const data = {
        code: 'telemarketing',
        groupId: val
      }
      // 电销员
      getUsersByGroupId(data).then(res => {
        this.salesGroupMemberIds = [];
        if (res.status === 200 && res.data && res.data.length > 0) {
          this.salesGroupMemberIds = res.data;
        }
      })
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch("saveElecSalesmanPage", {
        path: this.$route.path,
        listQuery: this.listQuery
      });
      report(this.listQuery).then(res => {
        if (res.status === 200) {
          this.listLoading = false;
          this.tableData = res.data.rows;
          this.total = Number(res.data.total);
        } else {
          this.$message({
            showClose: true,
            type: "warning",
            message: res.message
          });
        }
      });
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.listQuery.type = 2;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.listQuery.dateStr = "";
      this.listQuery.groupId = "";
      this.listQuery.checkUserId = "";
      this.listQuery.type = 2;
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
      width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  margin-left: 20px;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.href {
  border-bottom: 1px solid #000;
  cursor: pointer;
  &:hover {
    color: #20a0ff;
    border-bottom: 1px solid #20a0ff;
  }
}
</style>